package wyq.gy.fragment;


import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.SaveCallback;

import cn.iwgang.countdownview.CountdownView;
import wyq.gy.OrderDetailActivity;
import wyq.gy.R;
import wyq.gy.ReleaseOrderActivity;

public class CountDonwDlgFragment extends DialogFragment {
    private static String TAG = "CountDonwDlgFragment";

    public CountDonwDlgFragment() {

    }

    @Override
    public void onStart() {
        super.onStart();
        // 设置dialog的layout
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        WindowManager.LayoutParams layoutParams = getDialog().getWindow().getAttributes();
        layoutParams.width = dm.widthPixels;
        layoutParams.height = layoutParams.WRAP_CONTENT;
        layoutParams.gravity = Gravity.CENTER;
        getDialog().getWindow().setAttributes(layoutParams);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dialog_countdown, container, false);
        CountdownView mCvCountdownView = (CountdownView)view.findViewById(R.id.cv_countdownView);
        mCvCountdownView.setOnCountdownEndListener(new CountdownView.OnCountdownEndListener() {
            @Override
            public void onEnd(CountdownView cv) {
            String orderId = getArguments().getString("orderId");
            AVObject todo = AVObject.createWithoutData("MyOrderList", orderId);
            todo.put("Status","3");
            todo.saveInBackground(new SaveCallback() {
                @Override
                public void done(AVException e) {
                if (e == null) {
                    Toast.makeText(getActivity(), "抢单人员已确认", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
                }
                }
            });
            }
        });
        mCvCountdownView.start(5000);
        return view;
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);//设置actionbar的隐藏
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }
}
